//package com.cws.controller;
//
///**
// * [说明/描述]
// *
// * @author 陈新乐
// * @version 1.0
// * @date 2024/6/27
// */
//
///**
// * @author YuSong.Hu
// * @program partners-parent
// * @date 2022/9/20
// * @create 2022-09-20 11:10
// * @description ftp文件controller
// **/
//@Api(tags = "FTP文件控制器")
//@RestController
//@RequestMapping("/ftp")
//@Log4j2
//public class FTPController {
//
//    @Autowired
//    private ProjectFTP projectFTP;
//
//    @Autowired
//    private FileService fileService;
//
//    @ApiOperation(value = "文件下载", notes = "小文件系统")
//    @GetMapping("/download")
//    public void download(@RequestParam(required = false, defaultValue = "false") Boolean isNeedFileName, String path, HttpServletResponse httpServletResponse) throws Exception {
//        log.info("开始连接小文件系统FTP服务器");
//        FtpUtil ftp = null;
//        @Cleanup ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        try {
//            ftp = new FtpUtil(projectFTP.getUrl(), projectFTP.getPort(), projectFTP.getUserName(), projectFTP.getPassWord(), projectFTP.getTimeout());
//            ftp.login();
//            ftp.download(byteArrayOutputStream, path);
//            ftp.closeServer();
//        } catch (Exception e) {
//            throw new RuntimeException("文件下载失败:" + e.getMessage());
//        } finally {
//            if (ftp != null && ftp.serverIsOpen()) {
//                ftp.closeServer();
//            }
//        }
//        log.info("下载结束");
//        byte[] bytes = byteArrayOutputStream.toByteArray();
//        httpServletResponse.setContentType("application/x-msdownload");
//        if (isNeedFileName) {
//            File file = fileService.getOne(new LambdaQueryWrapper<File>().eq(File::getFilePath, path), false);
//            String fileName = "not found";
//            if (ObjectUtil.isNotEmpty(file)) {
//                fileName = file.getFileName();
//            }
//            httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"));
//        } else {
//            httpServletResponse.setHeader("Content-Disposition", "attachment; filename=");
//        }
//        @Cleanup ServletOutputStream outputStream = httpServletResponse.getOutputStream();
//        outputStream.write(bytes);
//        outputStream.flush();
//        outputStream.close();
//    }
//
//
//    @ApiOperation(value = "上传附件", notes = "上传到小文件系统")
//    @PostMapping("/upload")
//    public Result<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
//        long size = file.getSize();
//        String filename = file.getOriginalFilename();
//        @Cleanup InputStream inputStream = file.getInputStream();
//        String md5Str = SecureUtil.md5(inputStream);
//        String suffix = FileNameUtil.getSuffix(filename);
//        String dir = projectFTP.getDirPath() + DateUtil.today();
//        String fileName = IdUtil.getSnowflake().nextIdStr() + "." + suffix;
//        String filePath = dir + "/" + fileName;
//        FtpUtil ftp = null;
//        boolean uploadFlag = false;
//        try {
//            ftp = new FtpUtil(projectFTP.getUrl(), projectFTP.getPort(), projectFTP.getUserName(), projectFTP.getPassWord(), projectFTP.getTimeout());
//            ftp.login();
//            if (!ftp.isDirExist(dir)) {
//                ftp.createDir(dir);
//            }
//            ftp.changeDirectory(dir);
//            uploadFlag = ftp.upload(file.getInputStream(), fileName);
//            ftp.closeServer();
//            fileService.save(File.builder()
//                    .fileName(filename)
//                    .filePath(filePath)
//                    .fileMd5(md5Str)
//                    .fileSize(size + "")
//                    .build());
//        } catch (Exception e) {
//            throw new RuntimeException("文件上传失败:" + e.getMessage());
//        } finally {
//            if (ftp != null && ftp.serverIsOpen()) {
//                ftp.closeServer();
//            }
//        }
//        return Result.success(filePath, "附件上传结果:" + uploadFlag);
//    }
//
//    @ApiOperation(value = "文件下载", notes = "小文件系统/外部系统地址")
//    @GetMapping("/download/compatible")
//    public void download(@RequestParam String path, HttpServletResponse httpServletResponse) throws IOException {
//        Assert.notBlank(path, "路径为空");
//
//        @Cleanup ServletOutputStream outputStream = httpServletResponse.getOutputStream();
//        try {
//            if (path.startsWith("/")) {
//                this.download(true, path, httpServletResponse);
//            } else if (path.toLowerCase().startsWith("http")) {
//                HttpResponse execute = HttpUtil.createGet(path).execute();
//                Map<String, List<String>> headers = execute.headers();
//                headers.forEach((k, v) -> {
//                    httpServletResponse.setHeader(k, String.join(";", v));
//                });
//                byte[] bytes = execute.bodyBytes();
//                outputStream.write(bytes);
//                outputStream.flush();
//                outputStream.close();
//                HttpUtil.download(path, outputStream, true);
//            }
//        } catch (Exception e) {
//            throw new RuntimeException("文件下载失败;");
//        } finally {
//            if (outputStream != null) {
//                outputStream.close();
//            }
//        }
//    }
//}
